package org.zxstudio.encrypt;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.zxstudio.encrypt.utils.FileComparator;
import org.zxstudio.encrypt.utils.SDUtils;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * @author fan
 * 
 */
public class FileExplorerActivity extends Activity {
	private static final String TAG = "FileViewerActivity";
	private static boolean DEBUG = true;

	public static final String FILE_TYPE = "file_type";

	public static final int FILE_NONE = 0;
	public static final int FILE_TXT = 1;
	public static final int FILE_IMAGE = 2;
	public static final int FILE_AUDIO = 3;
	public static final int FILE_VIDEO = 4;

	private int mFileType = FILE_NONE;
	private List<File> mFilesList = new ArrayList<File>();

	private ExplorerFileTask mTask;

	private ListView mListView;
	private GridView mGridView;
	private FileBaseAdapter mAdapter;
	private TextView mEmptyView;
	private Button mBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.explorer_file_layout);
		init(getIntent());
	}

	private void init(Intent intent) {
		mListView = (ListView) findViewById(R.id.listview);
		mGridView = (GridView) findViewById(R.id.gridview);
		mAdapter = new FileBaseAdapter(this, mFilesList);
		mEmptyView = (TextView) findViewById(R.id.empty);
		setViewMode(FileBaseAdapter.VIEWMODE_ICON);
		mFileType = intent.getIntExtra(FILE_TYPE, FILE_NONE);
		mTask = new ExplorerFileTask();
		mTask.execute(mFileType);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onStart() {
		super.onStart();

	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	public static Intent createIntent(Context context, int viewId) {
		Intent intent = new Intent(context, FileExplorerActivity.class);
		int type = FILE_NONE;
		switch (viewId) {
		case R.id.txt_encrypt:
			type = FILE_TXT;
			break;
		case R.id.image_encrypt:
			type = FILE_IMAGE;
			break;
		case R.id.audio_encrypt:
			type = FILE_AUDIO;
			break;
		case R.id.video_encrypt:
			type = FILE_VIDEO;
			break;
		case R.id.sdcard:
			type = FILE_NONE;
			break;
		}
		intent.putExtra(FILE_TYPE, type);
		return intent;

	}

	/**
	 * explorer sd file by fileType
	 * 
	 * @author fan
	 * 
	 */
	private class ExplorerFileTask extends AsyncTask<Integer, Integer, Void> {

		private ProgressDialog dialog;

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			dialog = ProgressDialog.show(
					FileExplorerActivity.this,
					"",
					FileExplorerActivity.this.getResources().getString(
							R.string.explorer_sd_file));

		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			dialog.dismiss();

			if (mFilesList.size() > 0) {
				mEmptyView.setVisibility(View.GONE);
			} else {
				mEmptyView.setVisibility(View.VISIBLE);
			}
			
			mAdapter.notifyDataSetChanged();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected Void doInBackground(Integer... params) {
			int type = params[0];

			if (DEBUG) {
				Log.v(TAG, "explorer task execute file type = " + type);
			}
			if (SDUtils.isAvailableCurrentSD()) {
				if (type != FILE_NONE) {
					getFilesByType(SDUtils.getSdRootPath());
				} else {
					getFiles();
				}
			}
			return null;
		}

	}

	private void getFiles() {
		File path = SDUtils.getSdRootPath();
		File[] files = path.listFiles();
		Arrays.sort(files, new FileComparator());
		for (File file : files) {
			mFilesList.add(file);
		}

	}

	private void getFilesByType(File filePath) {
		// File filePath = SDUtils.getSdRootPath();
		if (filePath == null) {
			return;
		}
	//	filePath.m
		File[] files = filePath.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					getFilesByType(files[i]);
				} else {
					if (mFileType == FILE_TXT) {
						if (SDUtils.isTextFile(files[i])) {
							mFilesList.add(files[i]);
						}
					} else if (mFileType == FILE_AUDIO) {
						if (SDUtils.isAudioFile(files[i])) {
							mFilesList.add(files[i]);
						}

					} else if (mFileType == FILE_IMAGE) {
						if (SDUtils.isImageFile(files[i])) {
							mFilesList.add(files[i]);
						}

					} else if (mFileType == FILE_VIDEO) {
						if (SDUtils.isVideoFile(files[i])) {
							mFilesList.add(files[i]);
						}
					}
				}
			}
		}
	}

	private String[] fileToString(ArrayList<File> f) {
		ArrayList<String> listStr = new ArrayList<String>();
		for (int i = 0; i < f.size(); i++) {
			String nameString = f.get(i).getName();
			listStr.add(nameString);
		}
		// Arrays.sort(listStr);
		String[] str = listStr.toArray(new String[0]);
		Arrays.sort(str);
		return str;
	}

	private String[] fileToString(File[] files) {

		return null;

	}

	public void setViewMode(int viewmode) {
		switch (viewmode) {
		case FileBaseAdapter.VIEWMODE_LIST:
			mAdapter.setViewMode(FileBaseAdapter.VIEWMODE_LIST);
			mListView.setAdapter(mAdapter);
			mGridView.setAdapter(null);
			mGridView.setVisibility(View.GONE);
			mAdapter.notifyDataSetChanged();
			break;
		case FileBaseAdapter.VIEWMODE_ICON:
			mAdapter.setViewMode(FileBaseAdapter.VIEWMODE_ICON);
			mListView.setAdapter(null);
			mListView.setVisibility(View.GONE);
			mGridView.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
			break;
		}
	}

}
